<?

	//var $imageBlocks = array("68" => "leftImage","70" => "centreImage","69" => "rightImage");
	define("IMAGE_ALIGN_LEFT", 68);
	define("IMAGE_ALIGN_RIGHT",69);
	define("IMAGE_ALIGN_CENTRE",70);
	define("IMAGE_HEADER", 66);
	define("IMAGE_FOOTER", 67);

	class CmsImageContentBlock extends CmsContentBlock {



		//add the mapping here..
		function CmsImageContentBlock () {
			parent::CmsContentBlock();

			 $this->keys = array_merge_recursive(
			 	array(
			 		EXTRACT_STRING => array("imageTitle","description","caption","targetUrl")
			 	),

 				// EXTRACT_TO_MANY_OBJECTS and EXTRACT_TO_ONE_OBJECT // are evil see notes for load in CmsKVC::
			 	array(
			 		EXTRACT_TO_MANY_OBJECTS => array()
			 	),
			 	array(
			 		EXTRACT_TO_ONE_OBJECT => array()
			 	),
			 	array(
			 		EXTRACT_BOOLEAN => array("active")
			 	),
			 	array(
			 		EXTRACT_FILE_UPLOAD => array("image")
			 	),
			 	array(
			 		EXTRACT_NUMBER => array ("imageBlockPosition","fileTypeCode")//this is a simple type and not an object
			 	),$this->keys
			 );

			 $this->requiredKeys = array_merge(
			 	array( "imageTitle","image", "imageBlockPosition","fileTypeCode" ),
			 	$this->requiredKeys
			 );
			 $this->dataModelKeyMapping = array_merge(
			 	$this->dataModelKeyMapping,
			 	array(
			 		"imageTitle"=>"title",
			 		"description" => "shortdescription",
			 		"image" =>"content",
			 		"imageBlockPosition" => "extra1",
			 		"fileTypeCode" => "extra2",
			 		"caption"=>"value",
			 		"targetUrl"=>"url"			 		
			 	)
			 );
			$this->documentTypeCode ='imageBlock';
			$this->formId = 178;
		}




		function generateForm(&$db){
			$form = parent::generateForm($db);
			//we will use this to just print an image for the item thing.. perhaps..
			// the actual upload for at the moment is handled by an action in the parent container.

			//we check the cache or pull from the db..
			//we need a link to the image.. and thumnail thingee
			$id= $this->id();
			$image = $this->valueForKey('image');

			$mimetype=  $image['type'];
			$form .= <<< EOD
	<img type="$mimetype" src="imageContentBlockPreview.php?imageContentBlockId=$id&fileTypeCode=2" />
EOD;

			$captionId = $this->keyForField("caption");
			$caption = $this->valueForKey('caption');
			
			$targetUrlId = $this->keyForField("targetUrl");
			$targetUrl = $this->valueForKey('targetUrl');
						
			$form .= <<< EOD
			<br/>			
			<br/>
			Caption: <br/><input type="text" id="$captionId" name="$captionId" value="$caption" /><br/>
			Url: <br/><input type="text" id="$targetUrlId" name="$targetUrlId" value="$targetUrl" />	<br/><br/>		
EOD;

			return $form;
		}

		function generateHTML(&$db){
			$form = parent::generateHTML($db);
			$image = $this->valueForKey('image');
			$imageUrl=null;
			$alt=null;
			$id= $this->valueForKey('id');

			
			if (is_null($image)|| !is_array($image))
				return $form;
			
			$class = "";
			$align = "";
			switch($this->valueForKey('imageBlockPosition')){
				case IMAGE_ALIGN_LEFT:
					//$align="align=\"left\"";
					$class= "class=\"imageLeft\"";								
				break;
				case IMAGE_ALIGN_CENTRE:
					//$align="style=\"margin-right:auto; margin-left:auto;\"";
					$class= "class=\"imageCentre\"";							
				break;
				case IMAGE_ALIGN_RIGHT:
					//$align="align=\"right\"";
					$class= "class=\"imageRight\"";				
				break;
				case IMAGE_HEADER:
					$class = "class=\"imageHeader\"";
				break;
				case IMAGE_FOOTER:
					$class = "class=\"imageFooter\"";
				break;
			}

			if ($this->valueForKey('preview')){

			} else {
				$documentCode =	$image['documentCode'];
				$elementId = $image['elementId'];
				$fileTypeCode = $this->valueForKey('fileTypeCode');
				$imageUrl = CMS_HTTP_HOST . "/html/blob.php?documentCode=$documentCode&elementId=$elementId&filetypecode=$fileTypeCode";
			}

			$form .= "<img  src=\"$imageUrl\" $align alt=\"$alt\" id=\"$id\" $class/>";
			return $form;
		}

		//add additional check here for saved data aslo
		function hasBinaryData(){
			$fileData = $this->valueForKey('image');
			$fn = CMS_KVC_UPLOAD_TEMP_IMAGES . "/" . $this->valueForKey('id'). "/" . $this->valueForKey('id') . "." . imageFileExtensionForMimeType( $fileData['type']);
			return !is_null($fileData) && is_array($fileData) && file_exists($fn);//check for binary data on file system??
		}

		function takeValuesFromRequest() {
			parent::takeValuesFromRequest();
		}
		
		function validateForSave(){//PLEASE REMOVE ME.. this is here so we can debug the validate for save in binary files.
			$val = parent::validateForSave();
			return $val;
		}

	}